//
//  ChangeDefaultSound.m
//  iTimeline
//
//  Created by Anh_Tran on 1/15/13.
//  Copyright (c) 2013 Mobioneer Co., Ltd. All rights reserved.
//

#import "ChangeDefaultSound.h"
#import "Common.h"
@interface ChangeDefaultSound ()

@end

@implementation ChangeDefaultSound

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        change = -1;

        NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"ListSound" ofType:@"plist"];
        value = [NSArray arrayWithContentsOfFile:plistPath];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
    self.navigationItem.rightBarButtonItem = anotherButton;
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setTableView:nil];
    [super viewDidUnload];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [value count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        
        
    }
    
    cell.textLabel.text = [value objectAtIndex:indexPath.row];
    if (change == indexPath.row) {
        cell.accessoryType = UITableViewCellStyleSubtitle;
    }
    else
        cell.accessoryType = NAN;
    //cell.accessoryType = UITableViewCellStyleValue1;
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [audioPlayer stop];
    change = indexPath.row;
    [tableView reloadData];
    NSLog(@"value = %@",[value objectAtIndex:indexPath.row]);
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[value objectAtIndex:indexPath.row] ofType:@"mp3"]];
    
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
    NSError *error;
    
    audioPlayer = [[AVAudioPlayer alloc]
                   initWithContentsOfURL:url
                   error:&error];
    
    
        audioPlayer.delegate=self;
        audioPlayer.volume=10.0;
        if (error)
        {
            NSLog(@"Error in audioPlayer: %@",
              [error localizedDescription]);
        } else {
            audioPlayer.delegate = self;
            [audioPlayer prepareToPlay];
            [audioPlayer play];
        }
    
    
}
-(void)done{
    [audioPlayer stop];
    NSUserDefaults *sound =[NSUserDefaults standardUserDefaults];
    if ([sound objectForKey:@"sound"]){
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"sound"];
        [[NSUserDefaults standardUserDefaults]synchronize ];
    }
    [sound setObject:[value objectAtIndex:change] forKey:@"sound"];
    [sound synchronize];
    [self.navigationController popViewControllerAnimated:YES];
}
@end
